package com.xzsoft.gdwz.schedual;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.xzsoft.gdwz.common.HttpClientUtil;
import com.xzsoft.gdwz.model.TrdPsEmInfLog;
import com.xzsoft.gdwz.model.UserToken;
import com.xzsoft.gdwz.services.ConfirmOrCancleOrder;
import com.xzsoft.gdwz.services.JdOrderService;
import com.xzsoft.gdwz.services.OrderSchedual;
import com.xzsoft.gdwz.services.RefreshTokenServices;
import com.xzsoft.gdwz.services.TrdPsEmInfLogService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Component("jdOrderSchedual")
public class JdOrderSchedualImpl implements OrderSchedual {
	
	Logger logger = Logger.getLogger(JdOrderSchedualImpl.class);
	@Autowired
	private JdOrderService jdOrderService;
	
	@Autowired
	private RefreshTokenServices refreshTokenServices;
	
	@Autowired
	private TrdPsEmInfLogService trdPsEmInfLogService;
	
	@Autowired
	private ConfirmOrCancleOrder confirmOrCancleOrder;
	
	@Scheduled(cron="0 5/10 * * * ?")
	@Override
	public void getOrderInf() {
		UserToken userToken = refreshTokenServices.getToken("JD");
		String token = userToken.getAccessToken();
		String url = userToken.getOrderIdUrl();
		String del = "0";
		String type = "25";
		Map<String,String> map = new HashMap<String,String>();
		map.put("token", token);
		map.put("del", del);
		map.put("type", type);
		String result = HttpClientUtil.httpPost(url, map);
		//resultMessage
		logger.info("京东消息内容："+result);
		JSONObject jsonObject = JSONObject.fromObject(result);
		if("token已过期".equals(jsonObject.get("resultMessage"))){
			refreshTokenServices.refreshToken("JD");
			getOrderInf();
		}
		if(jsonObject.getBoolean("success")){
			String  orderResult = null;
			JSONArray jsonArray = JSONArray.fromObject(jsonObject.get("result"));
			Map paraMap = new HashMap<String,String>();
			paraMap.put("token", token);
			String getOrderUrl = userToken.getSelectOrderUrl();
			for(int i = 0;i<jsonArray.size();i++){
				JSONObject obj = (JSONObject)jsonArray.get(i);
				Long msgId = obj.getLong("id");
				JSONObject innerObj = JSONObject.fromObject(obj.get("result"));
				//订单号
				Long orderId = innerObj.getLong("orderId");
				//下单人
				String createBy = (String)innerObj.get("pin");
				//创建时间
				String createDate = obj.getString("time");
				paraMap.put("jdOrderId", orderId);
				orderResult = HttpClientUtil.httpPost(getOrderUrl, paraMap);
				logger.info("*********************:"+orderResult);
				JSONObject orderObj = JSONObject.fromObject(orderResult);
				if(orderObj.getBoolean("success")){
					JSONObject order = orderObj.getJSONObject("result");
					order.put("createBy", createBy);
					order.put("createDate", createDate);
					String jdorderid = null ;
					Double orderPrice = null ;
					if(order.getInt("type")==1){
						JSONObject pOrder = (JSONObject) order.get("pOrder");
						jdorderid = pOrder.get("jdOrderId")+"";
						orderPrice = (Double)pOrder.get("orderPrice");
						logger.info("电商订单"+jdorderid);
						logger.info("订单金额"+orderPrice);
					}else if(order.getInt("type")==2){
						jdorderid = (Long) order.get("jdOrderId")+"";
						logger.info("电商订单"+jdorderid);
						orderPrice = (Double) order.get("orderPrice");
						logger.info("订单金额"+orderPrice);
					}
					try {
						if(orderPrice<30000.00){
							//不保存订单
							confirmOrCancleOrder.JDConfirmOrder(jdorderid);
						}else{
							//保存訂單
							jdOrderService.saveOrder(order);
						}
						//刪除訂單消息
						String delUlr = userToken.getDelOrderIdUrl();
						Map delmap = new HashMap();
						delmap.put("id", msgId);
						delmap.put("token", token);
						String delReult = HttpClientUtil.httpPost(delUlr, delmap);
						logger.info("刪除消息："+delReult);
					} catch (Exception e) {
						e.printStackTrace();
						TrdPsEmInfLog log = new TrdPsEmInfLog();
						log.setId(UUID.randomUUID().toString());
						log.setCreateDate(createDate);
						log.setEmName("JD");
						log.setErrorMsg(e.getMessage());
						log.setHasRecieve("0");
						log.setOrderId(orderId+"");
						log.setOrderInfo(order.toString());
						trdPsEmInfLogService.insertTrdPsEmInfLog(log);
					}
				}
			}
		}
		
	}

}
